package com.far.permissions.interceptor;

import com.far.permisions.meta.function.FunctionValueConvertor;
import com.far.permisions.meta.groovy.GroovyValueConvertor;
import com.far.permisions.meta.FixedValueConvertor;
import com.far.permisions.rule.base.RuleResultInvoker;
import com.far.permisions.rule.YamlRuleConfigProvider;
import com.far.permisions.meta.function.FunctionValueHandler;
import com.far.permissions.rule.RuleValueConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class Config {
    @Autowired(required = false)
    List<FunctionValueHandler<?>> functionSuppliers;

    @Bean
    public DataPermissionInterceptor dataPermissionInterceptor() {
        YamlRuleConfigProvider yamlTableRuleProvider = new YamlRuleConfigProvider("test.yml");
        RuleResultInvoker ruleResultInvoker = new RuleResultInvoker(yamlTableRuleProvider);
        ruleResultInvoker.addValueActuator(functionValueActuator());
        ruleResultInvoker.addValueActuator(groovyValueActuator());
        ruleResultInvoker.addValueActuator(rawValueActuator());
        return new DataPermissionInterceptor(ruleResultInvoker);
    }

    public RuleValueConvertor functionValueActuator() {
        return new FunctionValueConvertor(functionSuppliers);
    }

    private RuleValueConvertor groovyValueActuator() {
        return new GroovyValueConvertor();
    }

    private RuleValueConvertor rawValueActuator() {
        return new FixedValueConvertor();
    }
}
